﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "MainWindowEventHandler.h"
#include "IApplicationWindowEvent.h"
#include "IApplicationWindowEventArgs.h"
#include "IApplication.h"
#include "IMainWindow.h"
#include "ILabelDefinition.h"
#include "IStatusBarPanel.h"
#include "IStatusBar.h"
#include "CommandIds.h"
#include "ISelection.h"
#include "ICommand.h"
#include "IDocumentManager.h"
#include "SelectionChangeEventHandler.h"
#include "ISelectionChangeEventArgs.h"
#include "ISelectionChangeEvent.h"
#include "UiPlatformUtils.h"
#include "IUiDocumentViewManager.h"
#include "IUiViewEvent.h"
#include "IUiDocumentEvent.h"
#include "DocumentEventHandler.h"
#include "UiViewEventHandler.h"
#include "UiDocumentEventHandler.h"
#include "CommandRegister.h"
#include "IRibbonContextualPage.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace Sample;
using namespace gcmp;

MainWindowEventHandler::MainWindowEventHandler()
{
    
}

MainWindowEventHandler::~MainWindowEventHandler()
{
    m_opPlatformUiBuilder.reset();
    IApplication* pApp = IApplication::Get();
    if (pApp)
    {
        IMainWindow* pMainWindow = IMainWindow::GetMainWindow();
        if (pMainWindow)
        {
            pMainWindow->GetMainWindowEvent()->Delete(this);
        }
    }
}

void MainWindowEventHandler::On(IApplicationWindowEventArgs* pArgs)
{
    if (pArgs->GetType() == ApplicationWindowEventType::PreFirstShow)
    {
        // 设置GCMP的BehaviorConfig文件路径，需在日志模块初始化之前
        //std::wstring behaviorConfigPath = FileSystem::GetExeDirPath() + L"gbmp_gcmp_behavior_config.xml";
        //FilePathManager::Get()->SetBehaviorConfigFilePath(behaviorConfigPath);
        SetUI();
        CommandRegManager::Get()->Setup();
    }
    else if(pArgs->GetType() == ApplicationWindowEventType::PreClose)
    {

    }
}

bool MainWindowEventHandler::SetUI()
{
    IMainWindow* pMainWnd = IMainWindow::GetMainWindow();
    if (pMainWnd == nullptr)
    {
        return false;
    }

    m_opPlatformUiBuilder = NEW_AS_OWNER_PTR(Sample::PlatformUiBuilder);
    m_opPlatformUiBuilder->Build();

    //响应选择变化
    gcmp::ISelection* pSelection = ISelection::Get();
    if (pSelection)
    {
        ISelectionChangeEvent* pSelectionEvent = pSelection->GetSelectionChangeEvent();
        if (pSelectionEvent)
        {
            pSelectionEvent->Add(SelectionChangeEventHandler::Get());
        }
    }
    //响应图形文档和视图变化
    m_opUiViewEventHandler = NEW_AS_OWNER_PTR(UiViewEventHandler);

    //文档事件处理器
    m_opUiDocumentEventHandler = NEW_AS_OWNER_PTR(UiDocumentEventHandler);
    m_opDocumentEventHandler = NEW_AS_OWNER_PTR(DocumentEventHandler);
    return true;
}